﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Web.Configuration;
using Oracle.DataAccess.Client;
using System.Data;
using Telerik.Web.UI;
using System.Text;

namespace sWarranty.WEB
{
    public partial class ShopINF : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnSearch.Focus();
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "ShopINF.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");


            btnRegisterNew.Attributes.Add("onclick", "openPopUp('ShopREG.aspx','open')");
            btnImport.Attributes.Add("onclick", "openPopUp('ShopREGxls.aspx','open')");
            string xx = "";

            if (!IsPostBack)
            {
                try
                {
                    xx = Session["StateSave"].ToString();
                }
                catch (Exception)
                {
                }
                if (!string.IsNullOrEmpty(xx))
                {
                    string[] mang = xx.Split('|');
                    BuildTable(mang[0].ToString(), mang[1].ToString(), mang[2].ToString(), mang[3].ToString(), null, null, mang[4].ToString(), mang[5].ToString());
                    Session["StateSave"] = "";
                    xx = "";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(xx))
                {
                    string[] mang = xx.Split('|');
                    BuildTable(mang[0].ToString(), mang[1].ToString(), mang[2].ToString(), mang[3].ToString(), null, null, mang[4].ToString(), mang[5].ToString());
                }
            }

        }

        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected string GetLocation(int districtID)
        {
            try
            {
                Entities db = new Entities();
                var di = from t in db.TB_CM_DISTRICT
                         where t.ID == districtID
                         select t;
                int _ProID = di.Single().PROVINCE_ID;
                var pro = from t in db.TB_CM_PROVINCE
                          where t.ID == _ProID
                          select t;
                return di.Single().NAME + ", " + pro.Single().NAME;
            }
            catch (Exception)
            {
                return "Error while get location";
            }

        }
        protected void BuildTable(string shopCode, string shopName, string shAdd, string shCell, string useFlag, string isDealer, string userRegion, string region)
        {
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SHOP_INQ", "uiroi", "p_shop_code", shopCode, "p_shop_name", shopName, "p_shop_address", shAdd, "p_shop_cell", shCell, "p_use_flag", useFlag, "p_dealer_yn", isDealer, "p_user_region", _currentUserRegion, "p_region_id", region, null, null
                , null, null, null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();

        }

        protected void btnImport_Click(object sender, EventArgs e)
        {

        }

        protected void btnRegisterNew_Click(object sender, EventArgs e)
        {
            //ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WIfsdfdNDOW", "window.open( 'ShopREG.aspx', null, 'width=' + window.screen.availWidth * 80 / 100 + ', height=' + window.screen.availHeight * 80 / 100 + ',status=yes,scrollbars=1,dependent=no,menubar=no,resizable=yes,center=yes' );", true);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string region = "%";
            string bill = null;
            string shopCode = null;
            string shopName = null;
            string cell = null;
            string address = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtBillTo.Text.Trim()))
                bill = txtBillTo.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopCode.Text.Trim()))
                shopCode = txtShopCode.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                shopName = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtCell.Text.Trim()))
                cell = txtCell.Text.Trim();
            if (!string.IsNullOrEmpty(txtAdd.Text.Trim()))
                address = txtAdd.Text.Trim();

            BuildTable(shopCode, shopName, address, cell, null, null, _currentUserRegion, region);

            //save sate to back after modify info
            Session["StateSave"] = shopCode + "|" + shopName + "|" + address + "|" +
                                   cell + "|" + _currentUserRegion + "|" + region;
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            string region = "%";
            string bill = null;
            string shopCode = null;
            string shopName = null;
            string cell = null;
            string address = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtBillTo.Text.Trim()))
                bill = txtBillTo.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopCode.Text.Trim()))
                shopCode = txtShopCode.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopName.Text.Trim()))
                shopName = txtShopName.Text.Trim();
            if (!string.IsNullOrEmpty(txtCell.Text.Trim()))
                cell = txtCell.Text.Trim();
            if (!string.IsNullOrEmpty(txtAdd.Text.Trim()))
                address = txtAdd.Text.Trim();
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SHOP_INQ", "uiroi", "p_shop_code", shopCode, "p_shop_name", shopName, "p_shop_address", address, "p_shop_cell", cell, "p_use_flag", null, "p_dealer_yn", null, "p_user_region", _currentUserRegion, "p_region_id", region, null, null
                , null, null, null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;

        }

        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
                (e.Column as GridBoundColumn).HeaderStyle.Font.Bold = true;
                (e.Column as GridBoundColumn).HeaderStyle.HorizontalAlign = HorizontalAlign.Center; 
            }
        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "data";
            RadGrid1.MasterTableView.ExportToExcel();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
        }
    }
}